package com.sjz.han.industry.activites;

import android.graphics.Bitmap;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sjz.han.industry.R;
import com.sjz.han.industry.base.BaseActivity;
import com.sjz.han.industry.entities.KeFuJsEntity;
import com.sjz.han.industry.entities.ProductEntity;
import com.sjz.han.industry.internetmanage.ReqestUrl;
import com.sjz.han.industry.utils.JsonUtils;
import com.sjz.han.industry.utils.PicUtils;
import com.sjz.han.industry.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

/**
 * 产品介绍
 */
public class ProductJsActivity extends BaseActivity implements View.OnClickListener{
    private TextView img_title_tv;
    private ImageView img_photo_img;

    private WebView mWebView;
    private ProgressBar progress_wheel;
    private String imagId="";
    @Override
    protected int setLayoutId() {
        return R.layout.activity_productjs;
    }

    @Override
    protected void initDatas() {
        imagId=getIntent().getStringExtra("imagId");
        getKeFu();
    }

    @Override
    protected void initViews() {
        reback_btn=(RelativeLayout)this.findViewById(R.id.reback_btn);
        titleTv=(TextView)this.findViewById(R.id.ctitle_tv);
        titleTv.setText("图文介绍");

        img_title_tv=(TextView)this.findViewById(R.id.img_title_tv);
        img_photo_img=(ImageView)this.findViewById(R.id.img_photo_img);

        mWebView=(WebView)this.findViewById(R.id.img_contnet);
        progress_wheel=(ProgressBar)this.findViewById(R.id.imgprogress_wheel);
    }
    private void setWebViewSetting(String content) {
        mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setAppCacheEnabled(true);
        mWebView.getSettings().setAllowFileAccessFromFileURLs(true);
        mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        mWebView.setVerticalScrollBarEnabled(false);
        mWebView.setVerticalScrollbarOverlay(false);
        mWebView.setHorizontalScrollBarEnabled(false);
        mWebView.setHorizontalScrollbarOverlay(false);
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        mWebView.getSettings().setDomStorageEnabled(true);
//        mWebView.loadUrl("http://www.baidu.com");
        mWebView.loadDataWithBaseURL(null, content,
                "text/html", "utf-8", null);
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                progress_wheel.setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
//                SysUtils.imgReset(mWebView);
                progress_wheel.setVisibility(View.GONE);
            }
        });
    }
    @Override
    protected void setLisener() {
        reback_btn.setOnClickListener(this);
    }

    @Override
    protected void onRetry() {
     getKeFu();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.reback_btn:
                finish();
                break;
        }
    }

    public void getKeFu(){
        showLoading();
        OkHttpUtils
                .post()
                .url(ReqestUrl.GONGLUE_Detila_URL)
                .addParams("token",shareUtils.getToken())
                .addParams("id",imagId)
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                showFaild();
                ToastUtils.popUpToast("请求超时");
            }
            @Override
            public void onResponse(String response, int id) {
                showSuccess();
                if(ReqestUrl.rebacRequestJson(response)!=null){
                    ProductEntity entity= JsonUtils.getObject(response,ProductEntity.class);
                    if(entity!=null&&entity.code==0){
                        if(entity.data.banner!=null){
                            setWebViewSetting(entity.data.banner.content);
                            img_title_tv.setText(entity.data.banner.name);
                            PicUtils.showImgRounded(ProductJsActivity.this, img_photo_img, ReqestUrl.BASE+entity.data.banner.imgUrl);
                        }
                    }else{
                        ToastUtils.popUpToast(entity.msg);
                    }
                }
            }
        });
    }
}
